/*
 * Copyright 2012 Fabian Wenzelmann
 * 
 * This file is part of Recognition-Procedures-for-Boolean-Functions.
 * 
 * Recognition-Procedures-for-Boolean-Functions is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Recognition-Procedures-for-Boolean-Functions is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Recognition-Procedures-for-Boolean-Functions.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.wenzelmf.booleanFunction.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Stack;

/**
 * A implementation of the {@link IStack} interface that just uses the Java stack class.
 * However it does not extend this class because the return types of <code>push</code>
 * are incompatible. But if we adjust the return type to <code>T</code> for this implementation
 * the {@link LinkedListStack} has the same problem because its type is <code>void</code>.
 * At some point we have to to grasp the nettle.
 * 
 * Because it simply uses all methods from Stack (with one exception that is the <code>isEmpty</code>
 * method that returns the value of {@link #empty()} this class is not documented,
 * see the <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Stack.html">Java documentation for the Stack class</a>.
 * <br>
 * So this implementation has a stack object and uses all the methods from it.
 * <br>
 * This class is a bridge between the {@link IStack} interface and the Java implementation.
 * 
 * @author Fabian Wenzelmann
 * @version 1.0
 *
 * @param <T> Type of the elements in the stack.
 */
public class JavaStack<T> implements IStack<T>
{
	private static final long serialVersionUID = -6420418628838537850L;
	
	/*
	 * Stores the elements in a Java stack.
	 */
	private Stack<T> elements;
	
	/**
	 * Creates a new empty Stack.
	 */
	public JavaStack()
	{
		this.elements = new Stack<T>();
	}
	
	/**
	 * Creates a new LinkedListStack, calls {@link #pushAll(Collection)} to
	 * add all elements from the collection.
	 * 
	 * @param c The elements the new created stack contains.
	 */
	public JavaStack(Collection<T> c)
	{
		this();
		pushAll(c);
	}
	
	@Override
	public boolean empty()
	{
		return elements.empty();
	}

	@Override
	public T peek()
	{
		return elements.peek();
	}

	@Override
	public T pop()
	{
		return elements.pop();
	}

	@Override
	public void push(T item)
	{
		elements.push(item);
	}
	
	public void pushAll(Collection<? extends T> c)
	{
		for(T e : c)
		{
			push(e);
		}
	}
	
	@Override
	public ListIterator<T> listIterator(int index)
	{
		return elements.listIterator(index);
	}

	@Override
	public int size()
	{
		return elements.size();
	}

	@Override
	public boolean add(T e)
	{
		return elements.add(e);
	}

	@Override
	public void add(int index, T element)
	{
		elements.add(index, element);
	}

	@Override
	public boolean addAll(Collection<? extends T> c)
	{
		return elements.addAll(c);
	}

	@Override
	public boolean addAll(int index, Collection<? extends T> c)
	{
		return elements.addAll(index, c);
	}

	@Override
	public void clear()
	{
		elements.clear();
	}

	@Override
	public boolean contains(Object o)
	{
		return elements.contains(o);
	}

	@Override
	public boolean containsAll(Collection<?> c)
	{
		return elements.containsAll(c);
	}

	@Override
	public T get(int index)
	{
		return elements.get(index);
	}

	@Override
	public int indexOf(Object o)
	{
		return elements.indexOf(o);
	}

	@Override
	public boolean isEmpty()
	{
		return empty();
	}

	@Override
	public Iterator<T> iterator()
	{
		return elements.iterator();
	}

	@Override
	public int lastIndexOf(Object o)
	{
		return elements.lastIndexOf(o);
	}

	@Override
	public ListIterator<T> listIterator()
	{
		return elements.listIterator();
	}

	@Override
	public boolean remove(Object o)
	{
		return elements.remove(o);
	}

	@Override
	public T remove(int index)
	{
		return elements.remove(index);
	}

	@Override
	public boolean removeAll(Collection<?> c)
	{
		return elements.removeAll(c);
	}

	@Override
	public boolean retainAll(Collection<?> c)
	{
		return elements.retainAll(c);
	}

	@Override
	public T set(int index, T element)
	{
		return elements.set(index, element);
	}

	@Override
	public List<T> subList(int fromIndex, int toIndex)
	{
		return elements.subList(fromIndex, toIndex);
	}

	@Override
	public Object[] toArray()
	{
		return elements.toArray();
	}

	@Override
	public <E> E[] toArray(E[] a)
	{
		return elements.toArray(a);
	}

	@Override
	public String toString()
	{
		return elements.toString();
	}
	
}
